/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.rest.client.utils;

import org.apache.commons.cli.CommandLine;

/**
 * This class validates the command line. It checks if everything necessary in
 * order to execute a request is present.
 *
 * @author smikloso
 */
public class CommandLineValidator {

    /**
     * Validate a command line.
     *
     * @param line command line to validate
     * @return true if command line is valid, false otherwise
     */
    public static boolean validate(CommandLine line) {
        if ((!line.hasOption("r") && !line.hasOption("w"))
                || (line.hasOption("r") && line.hasOption("w"))) {
            return false;
        }

        // WEAPON
        if (line.hasOption("w")) {
            if (!line.hasOption("o")) {
                return false;
            }
            String operation = line.getOptionValue("o");
            if (operation.equals("C")) {
                if (!line.hasOption("n")) {
                    return false;
                }
                if (line.hasOption("i")) {
                    return false;
                }
                if (line.hasOption("m")) {
                    try {
                        Long.parseLong(line.getOptionValue("m"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
                if (line.hasOption("g")) {
                    try {
                        Long.parseLong(line.getOptionValue("g"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("R")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("U")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
                if (line.hasOption("m")) {
                    try {
                        Long.parseLong(line.getOptionValue("m"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
                if (line.hasOption("g")) {
                    try {
                        Long.parseLong(line.getOptionValue("g"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("D")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("A")) {
                // All records query
            } else if (operation.equals("N")) {
                // Number of records query
            } else {
                // nothing like C, R, U, D, A or N
                return false;
            }
        }

        // REGION
        if (line.hasOption("r")) {
            if (!line.hasOption("o")) {
                return false;
            }
            String operation = line.getOptionValue("o");
            if (operation.equals("C")) {
                if (!line.hasOption("n")) {
                    return false;
                }
                if (line.hasOption("i")) {
                    return false;
                }
                if (line.hasOption("d")) {
                    if (line.getOptionValue("d").length() > 512) {
                        return false;
                    }
                }
                if (line.hasOption("a")) {
                    try {
                        Long.parseLong(line.getOptionValue("a"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("R")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("U")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
                if (!line.hasOption("n")) {
                    return false;
                }
                if (line.hasOption("d")) {
                    if (line.getOptionValue("d").length() > 512) {
                        return false;
                    }
                }
                if (line.hasOption("a")) {
                    try {
                        Long.parseLong(line.getOptionValue("a"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("D")) {
                if (!line.hasOption("i")) {
                    return false;
                } else {
                    try {
                        Long.parseLong(line.getOptionValue("i"));
                    } catch (NumberFormatException ex) {
                        return false;
                    }
                }
            } else if (operation.equals("A")) {
                // All records query
            } else if (operation.equals("N")) {
                // Number of records query
            } else {
                // nothing line C, R, U, D, A or N
                return false;
            }
        }
        return true;
    }
}
